package com.gfxy.modules.items.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gfxy.modules.common.entity.MemberList;
import com.gfxy.modules.items.entity.Kcszdlb;
import com.gfxy.modules.items.entity.teachingresult.TeachingResultContent;
import com.gfxy.modules.items.mapper.KcszdlbMapper;
import com.gfxy.modules.items.service.IKcszdlbService;
import com.gfxy.modules.items.vo.KcszdlbMemberPage;
import com.gfxy.modules.items.vo.KcszdlbPage;
import com.gfxy.modules.excel.ExcelUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.ImportExcelUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 课程思政大练兵
 * @Author: jeecg-boot
 * @Date:   2021-08-24
 * @Version: V1.0
 */
@Service
public class KcszdlbServiceImpl extends AbstractProjectServiceImpl<KcszdlbMapper, Kcszdlb> implements IKcszdlbService {
		private int memberCellNum=3;
//
//	@Override
//	public List<String> importExcel(MultipartFile file) throws IOException {
//		List<String> result = new ArrayList<>();
//		InputStream inputStream = file.getInputStream();
//		if (!(inputStream).markSupported()) {
//			inputStream = new PushbackInputStream(inputStream, 8);
//		}
//
//		Workbook workbook = WorkbookFactory.create(inputStream);
//
//		Sheet sheet = workbook.getSheetAt(0);
//		for(int i = 1; i<sheet.getLastRowNum();i++){
//			Row row = sheet.getRow(i);
//			String year = ExcelUtil.formatCell(row.getCell(0));
//			int classess = this.translateClassess(ExcelUtil.formatCell(row.getCell(1)));
//			int award = this.translateAward(ExcelUtil.formatCell(row.getCell(2)));
//
//			Kcszdlb kcszdlb = new Kcszdlb();
//			kcszdlb.setClassess(classess);
//			kcszdlb.setYear(year);
//			kcszdlb.setAward(award);
//
//
//			String name = ExcelUtil.formatCell(row.getCell(3));
//			List<MemberList> memberLists = memberListMapper.selectByRealName(name.trim());
//			MemberList resultMember = null;
//			if(memberLists.size()==1){
//
////				MemberList memberList = memberLists.get(0);
////				resultMember = memberList;
//				MemberList memberList = memberLists.get(0);
//				memberList.setRanked(1);
//				memberList.setProjectType("9");
//				memberList.setIsCustom("0");
//				memberList.setProportion(1.00);
//				resultMember = memberList;
//
//			} else if(memberLists.size() == 0){
//
//				result.add("教师:" + name + " 数据不存在，请手动处理");
//				MemberList memberList = new MemberList();
//				resultMember = memberList;
//			} else if(memberLists.size()>1){
//
//				result.add("教师:" + name + " 存在同名教师，请手动处理");
//				MemberList memberList = new MemberList();
//				resultMember = memberList;
//			}
//
//			double totalScore = this.getTotalScore(kcszdlb.getClassess(), kcszdlb.getAward());
//			kcszdlb.setTotalScore(totalScore);
//
//			this.saveMain(kcszdlb, resultMember);
//		}
//
//
//
//		if(result.size()==0){
//			result.add("导入成功");
//		}
//
//		return result;
//	}

//
//	@Override
//	public double getTotalScore(Integer classess, Integer award) {
//		double result = 0.0;
//		if(classess == 1){
//			if(award == 1){
//				result = 50;
//			} else if(award == 2 ) {
//				result = 30;
//			} else if(award == 3 ) {
//				result = 20;
//			}
//		} else if(classess == 2){
//			if(award == 1){
//				result = 50;
//			} else if(award == 2 ) {
//				result = 30;
//			} else if(award == 3 ) {
//				result = 20;
//			}
//		} else if(classess == 3){
//			if(award == 1){
//				result = 15;
//			} else if(award == 2 ) {
//				result = 10;
//			} else if(award == 3 ) {
//				result = 5;
//			}
//		}
//		return result;
//	}


	private int translateClassess(String name){
		if(name == null || name.equals("")){
			return 0;
		} else if(name.equals("国家级")){
			return 1;
		} else if(name.equals("省级")){
			return 2;
		} else if(name.equals("校级")){
			return 3;
		} else {
			return 0;
		}
	}

	private int translateAward(String name){
		if(name == null || name.equals("")){
			return 0;
		} else if(name.equals("教学标兵")){
			return 1;
		} else if(name.equals("教学能手")){
			return 2;
		}
//		else if(name.equals("三等奖")){
//			return 3;
//		}
		else {
			return 0;
		}
	}

	@Override
	protected void pass(String id, String isPass) {

	}

	@Override
	protected Kcszdlb getOneProject(Row row) {
		String classessStr = ExcelUtil.formatCell(row.getCell(1));
		String date = ExcelUtil.formatCell(row.getCell(0));
		final String award = ExcelUtil.formatCell(row.getCell(2));
		Kcszdlb jxms = new Kcszdlb();
		jxms.setClassess(translateClassess(classessStr));
		jxms.setAward(translateAward(award));
		jxms.setDate(date);
		jxms.setTotalScore(this.getTotalScore(jxms));

		return jxms;
	}

	@Override
	public Double getTotalScore(Kcszdlb kcszdlb) {
		Integer award = kcszdlb.getAward();
		Integer classess = kcszdlb.getClassess();
		double result = 0.0;
		if(classess == 1){
//			if(award == 1){
//				result = 50;
//			} else if(award == 2 ) {
//				result = 30;
//			}
//			else if(award == 3 ) {
//				result = 20;
//			}
		} else if(classess == 2){
			if(award == 1){
				result = 50;
			}
			else if(award == 2 ) {
				result = 30;
			}
//			else if(award == 3 ) {
//				result = 20;
//			}
		} else if(classess == 3){
			if(award == 1){
				result = 15;
			} else if(award == 2 ) {
				result = 10;
			}
//			else if(award == 3 ) {
//				result = 5;
//			}
		}
		return result;
	}
	@Override
	protected String[] getMemberArray(Row row) {
		String nameStrs = ExcelUtil.formatCell(row.getCell(memberCellNum));
		return nameStrs.split(memberNameSeparator);
	}

//	@Override
//	public Double[] getMemberListScore(double totalScore, int memberNum) {
//		return new Double[0];
//	}
@Override
public Page<KcszdlbMemberPage> jxmsPageListByYear(String date, Integer pageNo, Integer pageSize) {
	Page<KcszdlbMemberPage> page = new Page<>(pageNo,pageSize);
	List<KcszdlbMemberPage> kcszdlbMemberPages = this.baseMapper.selectKcszdlbVoListByYear(date);
	return page.setRecords(kcszdlbMemberPages);
}

	@Override
	public List<KcszdlbMemberPage> jxmsPageListByYear(String date) {
		return this.baseMapper.selectKcszdlbVoListByYear(date);
	}

	@Override
	protected List<TeachingResultContent> createTeachingResultContent(Kcszdlb jxcg)  {

		List<TeachingResultContent> result = new ArrayList<>();

		// 类型
		TeachingResultContent classTeachingResultContent = new TeachingResultContent();
		classTeachingResultContent.setType(1);
		classTeachingResultContent.setLevel(jxcg.getClassess());

		try {
			classTeachingResultContent.setValue(dictTranslater.getDictValue(KcszdlbPage.class, "classess", String.valueOf(jxcg.getClassess())));
			result.add(classTeachingResultContent);
		} catch (NoSuchFieldException e) {
			log.error(e.getMessage(),e);
		}

		//奖项
		TeachingResultContent awardTeachingResultContent = new TeachingResultContent();
		awardTeachingResultContent.setType(1);
		awardTeachingResultContent.setLevel(jxcg.getAward());
		try {
			awardTeachingResultContent.setValue(dictTranslater.getDictValue(KcszdlbPage.class, "award", String.valueOf(jxcg.getAward())));
			result.add(awardTeachingResultContent);
		} catch (NoSuchFieldException e) {
			log.error(e.getMessage(),e);
		}

		return result;
	}

	@Override
	public Result<?> importExcel(MultipartFile file) throws IOException {
		int SHEET_NUM = 0;
		int errorLines = 0;
		int successLines = 0;
		InputStream inputStream = file.getInputStream();
		if (!(inputStream).markSupported()) {
			inputStream = new PushbackInputStream(inputStream, 8);
		}
		Workbook book = WorkbookFactory.create(inputStream);
		Sheet sheet = book.getSheetAt(SHEET_NUM);
		List<String> errorMessage = new ArrayList<>();

		for (int i = START_ROW_NUM; i <= sheet.getLastRowNum(); i++) {
			ExcelResult excelResult = new ExcelResult(true,"");
			try{

				Row row = sheet.getRow(i);
				Kcszdlb t = this.getOneProject(row);

				List<MemberList> memberListList = this.getMemberList(row, t, excelResult);

				for (MemberList memberList : memberListList) {
					List<MemberList> memberLists = new ArrayList<>();
					memberList.setRanked(1);
					memberList.setScore(t.getTotalScore());
					memberLists.add(memberList);
					this.saveMain(t,memberLists);
					t.setId(null);
				}
			} catch ( Exception e){
				e.printStackTrace();
				log.error(e.getMessage(),e);
				excelResult.setErrorMsg("excel 第 " + i+ "行数据解析出现位置异常; ");
			}

			if(excelResult.isSuccess()){
				successLines++;
			} else {
				errorLines++;
				errorMessage.add(excelResult.getErrorMsg());
			}

		}

		return ImportExcelUtil.imporReturnRes(errorLines, successLines, errorMessage);
	}

}
